Design HashMap
Try to solve the Design HashMap problem.
We'll cover the following
Statement#
Design a hash map without using the built-in libraries. We only need to cater integer keys and integer values in the hash map. Return NULL if the key doesn’t exist.
It should support the following three primary functions of a hash map:
-
Put(key, value): This function inserts a key and value pair into the hash map. If the key is already present in the map, then the value is updated. Otherwise, it is added to the bucket.
-
Get(key): This function returns the value to which the key is mapped. It returns , if no mapping for the key exists.
-
Remove(key): This function removes the key and its mapped value.
Constraints:
-
key -
value - At most calls can be made to Put(), Get(), and Remove() functions.
Examples#
1 of 2
2 of 2
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Design HashMap
Consider the above hash map where the key-value pairs are mapped according to their respective hash keys, and the modulus base is 11. What will the value mapped to the key 10 be after the function below is called?
Put(10, 300)
Put(10, 200)
Put(10, 600)
Remove(10)
200
300
600
None of the above
After three successive put calls, the value that will be mapped to 10 is 600. When remove is called, both 10 and 600 are removed from the hash map.
Figure it out!#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in hash_map.py and bucket.py in the following coding playground.
Hash Maps: Introduction
Solution: Design HashMap